package com.itheima.prince.smartpeking.impl.NewsCenterPagers;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;
import com.itheima.prince.smartpeking.R;
import com.itheima.prince.smartpeking.base.BaseMenuPager;
import com.itheima.prince.smartpeking.bean.PhotosBean;
import com.itheima.prince.smartpeking.utils.ConstantUtil;
import com.squareup.picasso.Picasso;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2016/10/25 0025.
 */
public class PhotoDetailPager extends BaseMenuPager {
    List<PhotosBean.DataBean.NewsBean> news;
    private ListView mlv_photo_list;
    private GridView mgv_photo_grid;
    private boolean isList = true;
    private MyListAdapter myListAdapter;

    public PhotoDetailPager(Context context) {
        super(context);
    }

    @Override
    protected View initview() {
        View view = View.inflate(mcontext, R.layout.pager_photo_detail, null);
        mlv_photo_list = (ListView) view.findViewById(R.id.lv_photo_list);
        mgv_photo_grid = (GridView) view.findViewById(R.id.gv_photo_grid);

        return view;
    }

    @Override
    public void initData() {
        getFromData();
    }

    private void getFromData() {
        OkHttpUtils
                .get()
                .url(ConstantUtil.PHOTO_URL)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.d("aaa", "onError: 访问图片中心失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.d("aaa", "组图页面访问成功   " + response);
                        parseJson(response);
                    }
                });
    }

    private void parseJson(String response) {
        Gson gson = new Gson();
        PhotosBean photosBean = gson.fromJson(response, PhotosBean.class);
        String title = photosBean.getData().getNews().get(0).getTitle();
        Log.d("aaa", "parseJson: "+title);
        news = photosBean.getData().getNews();
        myListAdapter = new MyListAdapter();
        mlv_photo_list.setAdapter(myListAdapter);
    }

    // //组图界面的图片展示以及两种试图切换

    public void switchPhotoType(ImageButton mGvOrLv) {
        if (isList){
            mGvOrLv.setBackgroundResource(R.mipmap.icon_pic_list_type);
            mgv_photo_grid.setVisibility(View.VISIBLE);
            mlv_photo_list.setVisibility(View.GONE);
            mgv_photo_grid.setAdapter(myListAdapter);
            isList =!isList;
        }else {
            mGvOrLv.setBackgroundResource(R.mipmap.icon_pic_grid_type);
            mgv_photo_grid.setVisibility(View.GONE);
            mlv_photo_list.setVisibility(View.VISIBLE);
            mlv_photo_list.setAdapter(myListAdapter);
            isList =!isList;
        }
    }

    class MyListAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            if (news==null){
                return 0;
            }
            return news.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder myViewHolder;
            if (convertView==null){
                myViewHolder =new MyViewHolder();
                convertView = View.inflate(mcontext,R.layout.item_phonos,null);
                myViewHolder.mimageView = (ImageView) convertView.findViewById(R.id.iv_photos_image);
                myViewHolder.mtextView = (TextView) convertView.findViewById(R.id.tv_photos_text);
                convertView.setTag(myViewHolder);
            }else {
                myViewHolder= (MyViewHolder) convertView.getTag();
            }
            PhotosBean.DataBean.NewsBean newsBean = news.get(position);
            Picasso.with(mcontext).load(newsBean.getListimage()).into(myViewHolder.mimageView);
            myViewHolder.mtextView.setText(newsBean.getTitle());


            return convertView;
        }
    }

    public class MyViewHolder {
        ImageView mimageView;
        TextView mtextView;
    }


}
